package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.ITradingDataDAL;
import com.qire.manhua.model.enumeration.DetailType;
import com.qire.manhua.model.viewModel.impl.AccountDetailBeanViewModel;
import com.qire.manhua.model.vo.BeanExpenditureRecordVO;
import com.qire.manhua.model.vo.BeanExpiredRecordVO;
import com.qire.manhua.model.vo.BeanIncomeRecordVO;

public class AccountDetailBeanService extends DataService {

    private final ITradingDataDAL tradingDataDAL = WarehouseFactory.create(this, ITradingDataDAL.class);

    private final AccountDetailBeanViewModel viewModel;

    public AccountDetailBeanService(AccountDetailBeanViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 通过类型获取取明细记录
     * @param type 明细记录类型：支出、收入、过期
     * @param pageNo 页号
     */
    public void fetchBeanDetailRecordBy(DetailType type, int pageNo) {
        switch (type) {
            case EXPENDITURE:
                tradingDataDAL.fetchBeanExpenditureRecord(pageNo);
                return;
            case INCOME:
                tradingDataDAL.fetchBeanIncomeRecord(pageNo);
                return;
            case EXPIRED:
                tradingDataDAL.fetchBeanExpiredRecord(pageNo);
        }
    }
    @StrategyAnnotation(event = Url.BeanExpenditureRecordData)
    public void fetchBeanExpenditureRecordApiCallback(BeanExpenditureRecordVO[] dataVOs, boolean isCache) {
        viewModel.refreshLayoutFinish(300);
        if(ObjectUtil.isNull(dataVOs)) {
            return;
        }
//        if(ArrayUtil.isEmpty(dataVOs)) {
//            viewModel.noMoreDataWasShown();
//            return;
//        }

        for(BeanExpenditureRecordVO vo : dataVOs) {
            CharSequence title = String.format("%s《%s》%s", vo.prefix_title, vo.book_name, vo.chapter_name);
            CharSequence expiredTime = "";
            CharSequence recordTime = vo.create_time;
            CharSequence beanValue = vo.coin_bean;
            viewModel.addDetailRecordItem(title, expiredTime, recordTime, beanValue);
        }
        viewModel.refreshDetailRecordAdapter();

        if (!isCache) {
            viewModel.nextPage();
        }
    }
    @StrategyAnnotation(event = Url.BeanIncomeRecordData)
    public void fetchBeanIncomeRecordApiCallback(BeanIncomeRecordVO[] dataVOs, boolean isCache) {
        viewModel.refreshLayoutFinish(300);
        if(ObjectUtil.isNull(dataVOs)) {
            return;
        }
//        if(ArrayUtil.isEmpty(dataVOs)) {
//            viewModel.noMoreDataWasShown();
//            return;
//        }

        for(BeanIncomeRecordVO vo : dataVOs) {
            CharSequence title = vo.subject;
            CharSequence expiredTime = vo.expire_time;
            CharSequence recordTime = vo.create_time;
            CharSequence beanValue = vo.coin_bean;
            viewModel.addDetailRecordItem(title, expiredTime, recordTime, beanValue);
        }
        viewModel.refreshDetailRecordAdapter();

        if (!isCache) {
            viewModel.nextPage();
        }
    }
    @StrategyAnnotation(event = Url.BeanExpiredRecordData)
    public void fetchBeanExpiredRecordApiCallback(BeanExpiredRecordVO[] dataVOs, boolean isCache) {
        viewModel.refreshLayoutFinish(300);
        if(ObjectUtil.isNull(dataVOs)) {
            return;
        }
//        if(ArrayUtil.isEmpty(dataVOs)) {
//            viewModel.noMoreDataWasShown();
//            return;
//        }

        for(BeanExpiredRecordVO vo : dataVOs) {
            CharSequence title = vo.subject;
            CharSequence expiredTime = vo.expire_time;
            CharSequence recordTime = "";
            CharSequence beanValue = vo.coin_bean;
            viewModel.addDetailRecordItem(title, expiredTime, recordTime, beanValue);
        }
        viewModel.refreshDetailRecordAdapter();

        if (!isCache) {
            viewModel.nextPage();
        }
    }

}
